using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Xml.Serialization;

namespace NCover.Report
{
	[Serializable]
	public class CoverageFile
	{
		public CoverageFile()
		{
			//throw new ApplicationException("Your not ment to deserialise me....");
			//doing so for old report comparison.
		}

		public int PreviousHits;
		public int PreviousTotal;

		public void SetPrevious(CoverageFile prevFile)
		{
			PreviousHits = prevFile.HitCoveragePoints;
			PreviousTotal = prevFile.TotalCoveragePoints;
		}

		string _baseDir;
		public CoverageFile(string baseDir)
		{
			_baseDir = baseDir;
		}

		private ArrayList _coveragePoints = new ArrayList();

		public int HitCoveragePoints
		{
			get 
			{
				int hitCount = 0;
				NCover.NCoverCheck.Add(61); NCover.NCoverCheck.Add(60); NCover.NCoverCheck.Add(60); NCover.NCoverCheck.Add(60); NCover.NCoverCheck.Add(60); NCover.NCoverCheck.Add(60); NCover.NCoverCheck.Add(60); NCover.NCoverCheck.Add(60); NCover.NCoverCheck.Add(60); NCover.NCoverCheck.Add(60); NCover.NCoverCheck.Add(60); NCover.NCoverCheck.Add(60); NCover.NCoverCheck.Add(60); NCover.NCoverCheck.Add(60); foreach (CoveragePoint point in CoveragePoints)
				{
					if (point.Hit && NCover.NCoverCheck.Add(75) && NCover.NCoverCheck.Add(74) && NCover.NCoverCheck.Add(74) && NCover.NCoverCheck.Add(74) && NCover.NCoverCheck.Add(74) && NCover.NCoverCheck.Add(74) && NCover.NCoverCheck.Add(74) && NCover.NCoverCheck.Add(74) && NCover.NCoverCheck.Add(74) && NCover.NCoverCheck.Add(74) && NCover.NCoverCheck.Add(74) && NCover.NCoverCheck.Add(74) && NCover.NCoverCheck.Add(74) && NCover.NCoverCheck.Add(74)) { hitCount++; }
				}

				NCover.NCoverCheck.Add(62); NCover.NCoverCheck.Add(61); NCover.NCoverCheck.Add(61); NCover.NCoverCheck.Add(61); NCover.NCoverCheck.Add(61); NCover.NCoverCheck.Add(61); NCover.NCoverCheck.Add(61); NCover.NCoverCheck.Add(61); NCover.NCoverCheck.Add(61); NCover.NCoverCheck.Add(61); NCover.NCoverCheck.Add(61); NCover.NCoverCheck.Add(61); NCover.NCoverCheck.Add(61); NCover.NCoverCheck.Add(61); return hitCount;
			}
			set 
			{
				//for serialiser
			}
		}

		public int TotalCoveragePoints
		{
			get 
			{
				NCover.NCoverCheck.Add(63); NCover.NCoverCheck.Add(62); NCover.NCoverCheck.Add(62); NCover.NCoverCheck.Add(62); NCover.NCoverCheck.Add(62); NCover.NCoverCheck.Add(62); NCover.NCoverCheck.Add(62); NCover.NCoverCheck.Add(62); NCover.NCoverCheck.Add(62); NCover.NCoverCheck.Add(62); NCover.NCoverCheck.Add(62); NCover.NCoverCheck.Add(62); NCover.NCoverCheck.Add(62); NCover.NCoverCheck.Add(62); return CoveragePoints.Count;
			}
			set
			{
				//just for serialisation.
			}
		}

		public decimal TestCoveragePercentage 
		{
			get 
			{
				NCover.NCoverCheck.Add(64); NCover.NCoverCheck.Add(63); NCover.NCoverCheck.Add(63); NCover.NCoverCheck.Add(63); NCover.NCoverCheck.Add(63); NCover.NCoverCheck.Add(63); NCover.NCoverCheck.Add(63); NCover.NCoverCheck.Add(63); NCover.NCoverCheck.Add(63); NCover.NCoverCheck.Add(63); NCover.NCoverCheck.Add(63); NCover.NCoverCheck.Add(63); NCover.NCoverCheck.Add(63); NCover.NCoverCheck.Add(63); return ((decimal)HitCoveragePoints) / ((decimal)TotalCoveragePoints);		
			}
		}

		public string RelativePath
		{
			get 
			{
				NCover.NCoverCheck.Add(65); NCover.NCoverCheck.Add(64); NCover.NCoverCheck.Add(64); NCover.NCoverCheck.Add(64); NCover.NCoverCheck.Add(64); NCover.NCoverCheck.Add(64); NCover.NCoverCheck.Add(64); NCover.NCoverCheck.Add(64); NCover.NCoverCheck.Add(64); NCover.NCoverCheck.Add(64); NCover.NCoverCheck.Add(64); NCover.NCoverCheck.Add(64); NCover.NCoverCheck.Add(64); NCover.NCoverCheck.Add(64); return Path.GetDirectoryName(GetRelativePath(_baseDir, ((CoveragePoint) CoveragePoints[0]).Filename));
			}
			set 
			{
				//do nothing
			}
		}

		/// <summary>
		/// Returns the path relative to the publishing dir if possible.
		/// </summary>
		public static string GetRelativePath(string baseDir, string path)
		{
			if (path.StartsWith(baseDir) && NCover.NCoverCheck.Add(76) && NCover.NCoverCheck.Add(75) && NCover.NCoverCheck.Add(75) && NCover.NCoverCheck.Add(75) && NCover.NCoverCheck.Add(75) && NCover.NCoverCheck.Add(75) && NCover.NCoverCheck.Add(75) && NCover.NCoverCheck.Add(75) && NCover.NCoverCheck.Add(75) && NCover.NCoverCheck.Add(75) && NCover.NCoverCheck.Add(75) && NCover.NCoverCheck.Add(75) && NCover.NCoverCheck.Add(75) && NCover.NCoverCheck.Add(75))
			{
				NCover.NCoverCheck.Add(66); NCover.NCoverCheck.Add(65); NCover.NCoverCheck.Add(65); NCover.NCoverCheck.Add(65); NCover.NCoverCheck.Add(65); NCover.NCoverCheck.Add(65); NCover.NCoverCheck.Add(65); NCover.NCoverCheck.Add(65); NCover.NCoverCheck.Add(65); NCover.NCoverCheck.Add(65); NCover.NCoverCheck.Add(65); NCover.NCoverCheck.Add(65); NCover.NCoverCheck.Add(65); NCover.NCoverCheck.Add(65); return path.Substring(baseDir.Length + 1);
			}
			else
			{
				NCover.NCoverCheck.Add(67); NCover.NCoverCheck.Add(66); NCover.NCoverCheck.Add(66); NCover.NCoverCheck.Add(66); NCover.NCoverCheck.Add(66); NCover.NCoverCheck.Add(66); NCover.NCoverCheck.Add(66); NCover.NCoverCheck.Add(66); NCover.NCoverCheck.Add(66); NCover.NCoverCheck.Add(66); NCover.NCoverCheck.Add(66); NCover.NCoverCheck.Add(66); NCover.NCoverCheck.Add(66); NCover.NCoverCheck.Add(66); return "file:///" + path;
			}
		}

		public string Filename
		{
			get 
			{
				//Assert are all the same filename?
				return Path.GetFileName(((CoveragePoint) CoveragePoints[0]).Filename);
			}
			set 
			{
				//for serialisation
			}
		}

		[XmlArray]
		[XmlArrayItem(typeof(CoveragePoint))]
		public ArrayList CoveragePoints
		{
			get 
			{
				NCover.NCoverCheck.Add(68); NCover.NCoverCheck.Add(67); NCover.NCoverCheck.Add(67); NCover.NCoverCheck.Add(67); NCover.NCoverCheck.Add(67); NCover.NCoverCheck.Add(67); NCover.NCoverCheck.Add(67); NCover.NCoverCheck.Add(67); NCover.NCoverCheck.Add(67); NCover.NCoverCheck.Add(67); NCover.NCoverCheck.Add(67); NCover.NCoverCheck.Add(67); NCover.NCoverCheck.Add(67); NCover.NCoverCheck.Add(67); return _coveragePoints;
			}
			set
			{
				Hashtable added = new Hashtable();
				NCover.NCoverCheck.Add(69); NCover.NCoverCheck.Add(68); NCover.NCoverCheck.Add(68); NCover.NCoverCheck.Add(68); NCover.NCoverCheck.Add(68); NCover.NCoverCheck.Add(68); NCover.NCoverCheck.Add(68); NCover.NCoverCheck.Add(68); NCover.NCoverCheck.Add(68); NCover.NCoverCheck.Add(68); NCover.NCoverCheck.Add(68); NCover.NCoverCheck.Add(68); NCover.NCoverCheck.Add(68); NCover.NCoverCheck.Add(68); foreach (CoveragePoint point in value)
				{
					if (added[point] != null && NCover.NCoverCheck.Add(77) && NCover.NCoverCheck.Add(76) && NCover.NCoverCheck.Add(76) && NCover.NCoverCheck.Add(76) && NCover.NCoverCheck.Add(76) && NCover.NCoverCheck.Add(76) && NCover.NCoverCheck.Add(76) && NCover.NCoverCheck.Add(76) && NCover.NCoverCheck.Add(76) && NCover.NCoverCheck.Add(76) && NCover.NCoverCheck.Add(76) && NCover.NCoverCheck.Add(76) && NCover.NCoverCheck.Add(76))
					{
						NCover.NCoverCheck.Add(70); NCover.NCoverCheck.Add(69); NCover.NCoverCheck.Add(69); NCover.NCoverCheck.Add(69); NCover.NCoverCheck.Add(69); NCover.NCoverCheck.Add(69); NCover.NCoverCheck.Add(69); NCover.NCoverCheck.Add(69); NCover.NCoverCheck.Add(69); NCover.NCoverCheck.Add(69); NCover.NCoverCheck.Add(69); NCover.NCoverCheck.Add(69); NCover.NCoverCheck.Add(69); NCover.NCoverCheck.Add(69); throw new ApplicationException("Trying to add same coverage point twice. This should never happen.");
					}

					added[point] = point;
				}

				this._coveragePoints = value;
			}
		}

		private int[] MissedCoveragePointLineNumbers
		{
			get {
				int missCount = CoveragePoints.Count - HitCoveragePoints;
				int[] misses = new int[missCount];
				int currentMiss = 0;

				NCover.NCoverCheck.Add(71); NCover.NCoverCheck.Add(70); NCover.NCoverCheck.Add(70); NCover.NCoverCheck.Add(70); NCover.NCoverCheck.Add(70); NCover.NCoverCheck.Add(70); NCover.NCoverCheck.Add(70); NCover.NCoverCheck.Add(70); NCover.NCoverCheck.Add(70); NCover.NCoverCheck.Add(70); NCover.NCoverCheck.Add(70); NCover.NCoverCheck.Add(70); NCover.NCoverCheck.Add(70); NCover.NCoverCheck.Add(70); foreach (CoveragePoint point in CoveragePoints)
				{
					if (!point.Hit && NCover.NCoverCheck.Add(77) && NCover.NCoverCheck.Add(77) && NCover.NCoverCheck.Add(77) && NCover.NCoverCheck.Add(77) && NCover.NCoverCheck.Add(77) && NCover.NCoverCheck.Add(77) && NCover.NCoverCheck.Add(77) && NCover.NCoverCheck.Add(77))
					{
						misses[currentMiss++] = point.LineNumber;
					}
				}
				Array.Sort(misses);
				NCover.NCoverCheck.Add(72); NCover.NCoverCheck.Add(71); NCover.NCoverCheck.Add(71); NCover.NCoverCheck.Add(71); NCover.NCoverCheck.Add(71); NCover.NCoverCheck.Add(71); NCover.NCoverCheck.Add(71); NCover.NCoverCheck.Add(71); NCover.NCoverCheck.Add(71); NCover.NCoverCheck.Add(71); NCover.NCoverCheck.Add(71); NCover.NCoverCheck.Add(71); NCover.NCoverCheck.Add(71); NCover.NCoverCheck.Add(71); return misses;
			}
			set 
			{
				//for serialisation
			}
		}

		/// <summary>
		/// Returns a commer seperated list of line numbers where 
		/// coverage points were not hit by running the tests.
		/// </summary>
		public string MissedLineNumbers
		{
			get 
			{
				int[] missesList = MissedCoveragePointLineNumbers;

				StringBuilder misses = new StringBuilder();			
				NCover.NCoverCheck.Add(73); NCover.NCoverCheck.Add(72); NCover.NCoverCheck.Add(72); NCover.NCoverCheck.Add(72); NCover.NCoverCheck.Add(72); NCover.NCoverCheck.Add(72); NCover.NCoverCheck.Add(72); NCover.NCoverCheck.Add(72); NCover.NCoverCheck.Add(72); NCover.NCoverCheck.Add(72); NCover.NCoverCheck.Add(72); NCover.NCoverCheck.Add(72); NCover.NCoverCheck.Add(72); NCover.NCoverCheck.Add(72); foreach (int point in missesList)
				{
					if (misses.Length > 0 && NCover.NCoverCheck.Add(78) && NCover.NCoverCheck.Add(77) && NCover.NCoverCheck.Add(77) && NCover.NCoverCheck.Add(76) && NCover.NCoverCheck.Add(77) && NCover.NCoverCheck.Add(77) && NCover.NCoverCheck.Add(78) && NCover.NCoverCheck.Add(78) && NCover.NCoverCheck.Add(78) && NCover.NCoverCheck.Add(78) && NCover.NCoverCheck.Add(78) && NCover.NCoverCheck.Add(78) && NCover.NCoverCheck.Add(78) && NCover.NCoverCheck.Add(78))
					{
						misses.Append(", ");
					}

					misses.Append(point);
				}
				NCover.NCoverCheck.Add(74); NCover.NCoverCheck.Add(73); NCover.NCoverCheck.Add(73); NCover.NCoverCheck.Add(73); NCover.NCoverCheck.Add(73); NCover.NCoverCheck.Add(73); NCover.NCoverCheck.Add(73); NCover.NCoverCheck.Add(73); NCover.NCoverCheck.Add(73); NCover.NCoverCheck.Add(73); NCover.NCoverCheck.Add(73); NCover.NCoverCheck.Add(73); NCover.NCoverCheck.Add(73); NCover.NCoverCheck.Add(73); return misses.ToString();
			}
			set 
			{
				//for serialisation
			}
		}
	}
}














